//
//  XZMineViewController.m
//  XZ_WeChat
//
//  Created by 郭现壮 on 16/9/27.
//  Copyright © 2016年 gxz. All rights reserved.
//

#import "XZMineViewController.h"
#import "RegistViewController.h"

@interface XZMineViewController ()
@property (weak, nonatomic) IBOutlet UITextField *accountTF;
@property (weak, nonatomic) IBOutlet UITextField *passWordTF;
@property (weak, nonatomic) IBOutlet UILabel *stateLab;
@property (weak, nonatomic) IBOutlet UIButton *logOutBtn;
@property (weak, nonatomic) IBOutlet UIButton *registBtn;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;

@end

@implementation XZMineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.logOutBtn.hidden = YES;
    self.loginBtn.hidden =  NO;

    
    [[NSNotificationCenter defaultCenter]addObserverForName:kWebSocketDidOpenNote object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
       
        self.stateLab.text = @"连接成功";
    }];
    
    [[NSNotificationCenter defaultCenter]addObserverForName:kWebSocketDidCloseNote object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
       
        self.stateLab.text = @"未连接";

    }];
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
- (IBAction)loginClick:(id)sender {
    [self.view endEditing:YES];
    
    if ([_accountTF.text isEmptyString]||[_passWordTF.text isEmptyString]) {
        
        [MBHUD showHUDToView:self.view].setDark().showText(@"请输入账号密码");
        return;
    }
    
    
    [LZDSocketTool GetLoginWithParamer:@{@"username":_accountTF.text,@"password":_passWordTF.text} UserInfo:^(LZdUserModel * _Nonnull userM) {
       
        if (userM) {
            [self.imageView sd_setImageWithURL:[NSURL URLWithString:userM.avatar]];
            
            self.loginBtn.hidden =  YES;
            self.logOutBtn.hidden =  NO;
            
            
            
            [[SocketRocketUtility instance] SRWebSocketOpenWithURLString:[NSString stringWithFormat:@"%@?user_id=%@",API_webSocketAddress,userM.ID]];
            
            [[NSNotificationCenter defaultCenter]postNotificationName:LOGIN_SUCCESS object:@"111"];
        }
    }];
    
    
}

- (IBAction)logOutClick:(UIButton *)sender {
    self.loginBtn.hidden = NO;
    self.logOutBtn.hidden = YES;
    [[SocketRocketUtility instance]SRWebSocketClose];
    [[NSUserDefaults standardUserDefaults]removeObjectForKey:USER_ID];
      [[NSUserDefaults standardUserDefaults]synchronize];
    
    [[NSNotificationCenter defaultCenter]postNotificationName:LOGIN_SUCCESS object:@"111"];


}

- (IBAction)registClick:(id)sender {
    
    RegistViewController*vc = [[RegistViewController alloc]init];
    
    [self.navigationController pushViewController:vc animated:YES];
}

@end
